package cn.tangwei.org.controller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.org.domain.Department;
import cn.tangwei.org.query.DepartmentQuery;
import cn.tangwei.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  部门管理接口
 *      写接口的三要素：
 *           1.接口的请求地址是什么；
 *           2.请求参数是什么；
 *           3.返回值类型是什么。
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentContorller {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增或修改部门 put
     * 传对象就用@RequestBody
     * @param department
     * @return
     */
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxRusult addOrupdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除部门 delete
     * 传值就用@PathVariable("id")再josn里面拿数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxRusult del(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxRusult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 查询一个部门 get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询全部部门  get
     * @return
     */
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }
    /**
     * 查询部门树  get
     * @return
     */
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }

    /**
     * 分页加高级查询 post
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query, HttpServletRequest request){
        System.out.println(request.getHeader("token"));
        return departmentService.queryPage(query);
    }
}
